﻿using System; 
using System.Collections.Generic;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using System.Linq;
using System.Data;
using System.Reflection;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataColumnCategoryInfoELBDao : DaoService<DataColumnCategoryInfo>
    { 
        public override string TableName => "FAS_DataColumnCategoryInfo";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataObjectID";

        public override List<string> ExceptColumnName { get; } = DaoUtil.GetExceptionColumn<DataColumnCategoryInfo>();

        public DataColumnCategoryInfoELBDao() : base()
        {
            DBName = "TFAS";
        }

        //public override Dictionary<string, object> GetDataMapping(DataColumnCategoryInfo data)
        //{
        //    var datas = base.GetDataMapping(data);
        //    datas.Remove(nameof(DataColumnCategoryInfo.Disabled));
        //    datas.Add(nameof(DataColumnCategoryInfo.Disabled), data.Disabled ? 1 : 0);

        //    datas.Remove(nameof(DataColumnCategoryInfo.IsDefault));
        //    datas.Add(nameof(DataColumnCategoryInfo.IsDefault), data.IsDefault ? 1 : 0);
        //    return datas;
        //}

        public override DataColumnCategoryInfo ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove(nameof(DataColumnCategoryInfo.Disabled));
            myCol.Remove(nameof(DataColumnCategoryInfo.IsDefault));
            var data = base.ConvertToObject(myCol, row); 

            string disabled = row[nameof(DataColumnCategoryInfo.Disabled)]?.ToString();
            if (!string.IsNullOrEmpty(disabled))
            {
                if (int.TryParse(disabled, out int disabledInt))
                {
                    data.Disabled = disabledInt == 0 ? false : true;
                }
                else
                    data.Disabled = false;
            }
            else
                data.Disabled = false;

            string isDefault = row[nameof(DataColumnCategoryInfo.IsDefault)]?.ToString();
            if (!string.IsNullOrEmpty(isDefault))
            {
                if (int.TryParse(isDefault, out int isDefaultInt))
                {
                    data.IsDefault = isDefaultInt == 0 ? false : true;
                }
                else
                    data.IsDefault = false;
            }
            else
                data.IsDefault = false;
            return data;
        }


        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataColumnCategoryInfo> GetDataColumnCategoryInfos(string doId)
        {
            var result = new List<DataColumnCategoryInfo>();
            if (string.IsNullOrEmpty(doId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{doId}'");
            if (objects != null)
            {
                foreach (var obj in objects)
                {
                    if (obj != null)
                        result.Add(obj as DataColumnCategoryInfo);
                }
            }
            return result;
        }

        /// <summary>
        /// 添加DataColumnCategoryInfo
        /// </summary>
        /// <param name="DataColumnCategoryInfos"></param>
        public void CreateDataColumnCategoryInfo(List<DataColumnCategoryInfo> categoryInfos)
        {
            try
            {
                //如果ID或DataObjectID为空，则认为当前PresetData为空，不执行保存
                if (categoryInfos == null)
                    return;
                base.Add(categoryInfos);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.CreatePresetDataError, $"保存字段分类信息失败", e);
            }
        }

        /// <summary>
        /// 删除DataColumnCategoryInfo
        /// </summary>
        /// <param name="DataColumnCategoryInfos"></param>
        public void DeleteDataColumnCategoryInfo(string doId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, doId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.DeletePresetDataError, e.Message, e);
            }
        }
    }
}
